package kr.co.mcshop.admin;


import java.io.File;
import java.io.IOException;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.mcshop.option.OptionService;
import kr.co.mcshop.product.ProductService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping(value="/admin")
@Controller
public class AdminProductController {

	@Autowired
	ProductService ps;
	
	@Autowired
	OptionService os;

	
	@Value("${file.upload.path}")
	private String fileUploadPath;
	
	
	@RequestMapping(value="/productInsertForm")
	public String productInsertForm(@RequestParam Map map,Model model){
		List option = os.getAllOption(map);
		model.addAttribute("Option", option);
		return "admin/product/ProductInsertForm";
	}
	
	@RequestMapping(value="/productInsert", method = RequestMethod.POST)
	public String ProductInsert(@RequestParam(value = "file") List<MultipartFile> file, @RequestParam Map map) throws IllegalStateException, IOException{
		String image = "";
		for(int i=0; i<file.size(); i++){
			if(file.get(i).getOriginalFilename()==""){continue;}
		    File newFile = new File(fileUploadPath + file.get(i).getOriginalFilename());
		    System.out.println(newFile.getPath());
			file.get(i).transferTo(newFile);
			image += file.get(i).getOriginalFilename() + "/";
		}
		map.put("image", image);
		//System.out.println(map);
		ps.insertProduct(map);
		return "redirect:/admin/productListForm";
	}
	
	@RequestMapping(value="/productListForm")
	public String ProductListForm(@RequestParam Map map, Model model){
		List productList = (List) ps.getAllProduct(map);
		System.out.println(productList);
		model.addAttribute("ProductList", productList);
		 return "admin/product/ProductListForm";
	}
	
	@RequestMapping(value="/productEditForm")
	public String ProductEditForm(@RequestParam Map map, Model model){
		List productList = (List) ps.getAllProduct(map);
		//수정 선택된 상품의 상세목록을 위한 리스트
		
		HashMap<Object, Attribute> hash = new HashMap<Object, Attribute>();
		hash.put("no", null); //"no"값을 null로 줘서 전체 OptionList를 뽑아오기 위한, 파라메터 설정.
		List detailList = os.getAllOption(hash);
		//선택된 상품의 옵션목록을 가져오기 위한 리스트; 
		
		Map mapforImage = (Map) productList.get(0);
		String[] image = mapforImage.get("IMAGE").toString().split("[/]");
		
		if(image.length!=0){model.addAttribute("image", image);}
		else{model.addAttribute("image", "");}
		model.addAttribute("ProductList", productList);
		model.addAttribute("detailList", detailList);
		
		return "/admin/product/ProductEditForm";
	} 
	
	@RequestMapping(value="/productEdit")
	public String ProductEdit(@RequestParam(value = "file") List<MultipartFile> file, @RequestParam Map map) throws IllegalStateException, IOException{
		System.out.println(map);
		
		List productList = (List) ps.getAllProduct(map);
		Map list = (Map)productList.get(0);
		System.out.println(list);
		String[] str = list.get("IMAGE").toString().split("[/]"); 
		
		String image ="";
		if( file != null){
			for(int i=0; i<file.size(); i++){
			    File newFile = new File(fileUploadPath + file.get(i).getOriginalFilename());
			    file.get(i).transferTo(newFile);
				image += file.get(i).getOriginalFilename() + "/";
			}
		}
		map.put("IMAGE", image);
		//수정폼에서 업로드된 사진들의 이름을 DB에 저장
		
		ps.updateProduct(map);
		return "redirect:/admin/productEditForm?no="+list.get("NO");
	}
	
	@RequestMapping(value="/productDelete")
	public String productDelete(@RequestParam Map map){
		ps.deleteProduct(map);
		return "redirect:/admin/productListForm";
	}
}
